package com.epoint.mobileoa.personalized.epointcompany.ui.kaoqing;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;

import com.epoint.mobileoa.datanalysis.XMLManager;

public class KaoQingDataHandler {
	private List<KaoQingInfo> list = new ArrayList<KaoQingInfo>();
	private KaoQingInfo info;

	public List<KaoQingInfo> getData(String responses) throws Exception {
		XmlPullParser parser = XMLManager.getInstance();
		StringReader reader = new StringReader(responses);
		parser.setInput(reader);

		// 遍历报文
		while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
			// 开始节点
			if (parser.getEventType() == XmlPullParser.START_TAG) {
				if ("Record".equalsIgnoreCase(parser.getName())) {
					info = new KaoQingInfo();
				} else if ("AttendTime".equalsIgnoreCase(parser.getName())) {
					info.time = parser.nextText();
				} else if ("AttendLocation".equalsIgnoreCase(parser.getName())) {
					info.address = parser.nextText();
				}
			}

			// 结束节点
			else if (parser.getEventType() == XmlPullParser.END_TAG) {
				if ("Record".equalsIgnoreCase(parser.getName())) {
					list.add(info);
				}
			}
			// 跳转到下一个节点
			parser.next();
		}
		return getSortList(list);
	}

	private List<KaoQingInfo> getSortList(List<KaoQingInfo> infos) {
		Collections.sort(infos);
		return infos;
	}

}
